<template>
  <div class="message">
    <div>
      <ActionBar
        :add-active="addActive"
        :has-cancel="false"
        :has-edit="false"
        :has-del="false"
        :has-save="false"
        @add="addShow=true"
      />
    </div>
    <div class="list__wrapper">
      <el-table v-loading="loading" :data="tableData" element-loading-background="rgba(0, 0, 0, 0.8)">
        <el-table-column type="index" width="50" />
        <el-table-column prop="id" label="应用ID" />
        <el-table-column prop="name" label="应用名称" />
        <el-table-column prop="type" label="应用类型">
          <template slot-scope="scope">
            {{ typeList.find(i=>i.value===scope.row.type).label }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 0"
              size="mini"
              type="primary"
              @click="handleData(scope.row)"
            >处理</el-button>
            <!-- <el-button size="mini" type="primary" @click="toDetail(scope.row)">进入</el-button> -->
            <el-button size="mini" type="primary" @click="toDetail(scope.row)">探针</el-button>
            <el-button size="mini" type="warning" @click="editData(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="delData(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="page"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </div>
    <AddEdit v-model="addShow" :type-list="typeList" :current-item="currentItem" @close="closeDia" @reflash="getList" />
    <el-dialog
      title="探针"
      :visible.sync="detailShow"
      width="50%"
      top="5vh"
      :before-close="() => { detailShow = false }"
    >
      <pre class="inner-code"> {{ insertCode }}</pre>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import { getToken } from '@/utils/auth';
import Pagination from '@/components/Pagination';
import { debounce } from '@/utils/index';
import ActionBar from "@/components/ActionBar/index";
import AddEdit from './components/AddEdit.vue'
export default {
  name: 'MonitorProject',
  components: {
    Pagination,
    ActionBar,
    AddEdit
  },
  data() {
    return {
      insertCode: '',
      detailShow: false,
      addShow: false,
      currentItem: null,
      addActive: true,
      delActive: false,
      form: {
        remark: ''
      },
      dialogVisible: false,
      froms: [
        {
          label: '解决方案',
          value: 1
        },
        {
          label: '联系我们',
          value: 2
        }
      ],
      status: [
        {
          label: '全部',
          value: ''
        },
        {
          label: '未处理',
          value: 0
        },
        {
          label: '已处理',
          value: 1
        }
      ],
      searchFilter: {
        name: '',
        phone: '',
        email: '',
        type: 1,
        status: ''
      },
      page: 1,
      pageSize: 10,
      tableData: null,
      totalSize: 0,
      loading: false,
      currData: null,
      typeList: [{
        label: 'H5/PC/Web项目',
        value: '1'
      },
      {
        label: 'Uni-App',
        value: '2'
      }]
    };
  },
  computed: {
    databaseName() {
      return getToken('databaseName');
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleData(data) {
      this.currData = data;
      this.dialogVisible = true;
    },
    debounceSearch: debounce(function () {
      this.search();
    }, 250),
    search() {
      this.page = 1;
      this.getList();
    },
    async getList() {
      const { page, pageSize } = this;
      this.loading = true;
      const res = await axios({
        method: 'post',
        url: this.$webSerUrlPro + 'config/project/queryPageList',
        headers: {
          databaseName: this.databaseName
        },
        data: {
          page,
          size: pageSize
        }
      });
      this.loading = false;
      this.totalSize = res.data.data.totalElements;
      this.tableData = res.data.data.content;
      if (res.data.data.content.length === 0 && this.page !== 1) {
        this.currentPage--
        this.getList()
      }
    },
    editData(currData) {
      this.currentItem = JSON.parse(JSON.stringify(currData));
      this.addShow = true
    },
    delData(currData) {
      this.$confirm('是否确定此操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await axios({
            method: 'get',
            url: this.$webSerUrlPro + 'config/project/del',
            headers: {
              databaseName: this.databaseName
            },
            params: {
              id: currData.id
            }
          });
          this.getList();
        })
        .catch(() => { });
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getList();
    },
    changeCurrentPage(currentPage) {
      this.page = currentPage;
      this.getList();
    },
    closeDia() {
      this.addShow = false
      setTimeout(() => {
        this.currentItem = null
      }, 250);
    },
    toDetail(row) {
      this.insertCode = `
      // 复制到<head>的最上方
      <script src="https://matefarm.net/ywManage/ua-parser-js.js"><\/script>
      <script src="https://matefarm.net/ywManage/errorMonitor.js"><\/script>
      <script src="https://matefarm.net/ywManage/httpInterceptor.js"><\/script>
      <script>
      if(window.zkdtMonitor){
      // 填写对应的 项目信息
      window.zkdtMonitor.init({
        // 用户id
        userId: '',
        // 用户名称
        userName: '',
        // 项目ID,无需改动
        proID: '${row.id}',
        // 版本号
        proVersion: '',
        // 环境
        env: 'pro',
      })
      }
      <\/script>
      `;
      this.detailShow = true
    }
  }
};
</script>

<style lang="scss" scoped>
.message {
  overflow-y: auto;

  .search__filter {
    background: #00171c;
    flex-shrink: 0;
    padding: 10px;
    margin-bottom: 10px;
  }

  .pagination {
    text-align: right;
    position: static;
    margin-top: 10px;
  }

  .el-form-item {
    &:last-child {
      margin-bottom: 0;
    }
  }
}
.inner-code{
  text-align: left;
}
</style>
